package org.bambrikii.web.site.services.dictionary2;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.bambrikii.dictionary.Article;
import org.bambrikii.site.base.business.managers.core.ManagerException;
import org.bambrikii.site.base.model.Pager;
import org.bambrikii.site.base.struts2.core.SiteBasePagerAction;

public class Dictionary2Action extends SiteBasePagerAction<Pager<Article>> {
	private static final long serialVersionUID = -561429579246607420L;

	private Dictionary2Bean dictionary2Bean;

	private Integer articleId;

	private String title;
	private String body;

	private String sourceLocation;

	private Integer wordId;
	private String word;
	private String meaning;

	public Dictionary2Bean getDictionary2Bean() {
		return dictionary2Bean;
	}

	public void setDictionary2Bean(final Dictionary2Bean dictionary2Bean) {
		this.dictionary2Bean = dictionary2Bean;
	}

	public Integer getArticleId() {
		return articleId;
	}

	public void setArticleId(final Integer articleId) {
		this.articleId = articleId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(final String title) {
		this.title = title;
	}

	public String getBody() {
		return body;
	}

	public void setBody(final String body) {
		this.body = body;
	}

	public String getSourceLocation() {
		return sourceLocation;
	}

	public void setSourceLocation(final String sourceLocation) {
		this.sourceLocation = sourceLocation;
	}

	public Integer getWordId() {
		return wordId;
	}

	public void setWordId(final Integer wordId) {
		this.wordId = wordId;
	}

	public String getWord() {
		return word;
	}

	public void setWord(final String word) {
		this.word = word;
	}

	public String getMeaning() {
		return meaning;
	}

	public void setMeaning(final String meaning) {
		this.meaning = meaning;
	}

	//

	private Map<String, Map<Integer, String>> words;

	public Map<String, Map<Integer, String>> getWords() {
		return words;
	}

	public void setWords(final Map<String, Map<Integer, String>> words1) {
		words = words1;
	}

	private List<WordContext> wordsContexts = new LinkedList<WordContext>();

	public List<WordContext> getWordsContexts() {
		return wordsContexts;
	}

	public void setWordsContexts(final List<WordContext> wordsContexts) {
		this.wordsContexts = wordsContexts;
	}

	// Actions

	@Override
	public String execute() {
		try {
			setPager(getDictionary2Bean().getArticlesPager(getTitle(), "insertionTime DESC", getRequestedPage()));
		} catch (final ManagerException e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	public String loadArticle() throws ManagerException {
		final Article article = getDictionary2Bean().getArticle(getArticleId());
		setTitle(article.getTitle());
		setBody(article.getBody());
		setSourceLocation(article.getSourceLocation());

		final Map<String, Map<Integer, String>> words1 = getDictionary2Bean().getContexts(article);
		setWords(words1);

		return execute();
	}

	public String saveArticle() throws ManagerException {
		Article article = null;
		if (getArticleId() != null) {
			article = getDictionary2Bean().saveArticle(getArticleId(), getTitle(), getBody(), getSourceLocation());
		} else {
			article = getDictionary2Bean().saveArticle(getSourceLocation(), getTitle(), getBody());
		}
		setArticleId(article.getId());
		return SUCCESS;
	}

	public String saveContext() throws ManagerException {
		getDictionary2Bean().saveWord(getArticleId(), getWord(), getMeaning());
		return loadArticle();
	}

	public String saveContexts() throws ManagerException {
		final Map<String, String> wordsContexts2 = new HashMap<String, String>();
		final List<WordContext> wordsContextsVar = getWordsContexts();
		for (final WordContext word1 : wordsContextsVar) {
			wordsContexts2.put(word1.getWord(), word1.getContext());
		}
		getDictionary2Bean().saveContexts2(getArticleId(), wordsContexts2);
		return loadArticle();
	}

	public static String encodeBase64(final String str1) {
		return new String(Base64.encodeBase64(str1.getBytes()));
	}
}
